﻿using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine;
using UnityEngine.UI;

public class LoginInput : MonoBehaviour
{
    [Header("IP地址输入框")]
    public InputField ifIp;
    [Header("服务器端口号输入框")]
    public InputField ifPort;
    [Header("账号输入框")]
    public InputField ifAccount;
    [Header("密码输入框")]
    public InputField ifPwd;

    public GameObject loginInteface;

    void Start()
    {
        if(ifIp == null)
        {
            Debug.LogWarning("IP输入框没有对象");
        }
        if (ifPort == null)
        {
            Debug.LogWarning("端口输入框没有对象");
        }
        if (ifAccount == null)
        {
            Debug.LogWarning("账号输入框没有对象");
        }
        if (ifPwd == null)
        {
            Debug.LogWarning("密码输入框没有对象");
        }
    }



    public void Close()
    {
        if (loginInteface != null)
        {
            loginInteface.SetActive(false);
            ifPwd.text = "";
        }
    }

    public void Open(bool isOpen)
    {
        if (loginInteface != null)
        {
            loginInteface.SetActive(true);
        }

        loginInteface.GetComponent<Image>().enabled = isOpen;
    }

    /// <summary>
    /// 获取IP地址
    /// </summary>
    /// <param name="ips">存IP地址</param>
    /// <returns>成功/失败</returns>
    public bool GetIp(ref string ips)
    {
        if(ifIp!=null)
        {
            string ip = ifIp.text;
            if(VerificationIP(ip))
            {
                ips = ip;
                return true;
            }
        }
        return false;
    }

    /// <summary>
    /// 获取端口号
    /// </summary>
    /// <param name="ports">存端口号</param>
    /// <returns>成功/失败</returns>
    public bool GetPort(ref string ports)
    {
        if (ifPort != null)
        {
            string port = ifPort.text;
            if (VerificationPort(port))
            {
                ports = port;
                return true;
            }
        }
        return false;
    }

    /// <summary>
    /// 获取用户名
    /// </summary>
    /// <param name="names">存名字</param>
    /// <returns>成功/失败</returns>
    public bool GetAccount(ref string names)
    {
        if (ifAccount != null)
        {
            string account = ifAccount.text;
            if (VerificationUserInfo(account))
            {
                names = account;
                return true;
            }
        }
        return false;
    }
    /// <summary>
    /// 获取密码
    /// </summary>
    /// <param name="pwds">存名字</param>
    /// <returns>成功/失败</returns>
    public bool GetPwd(ref string pwds)
    {
        if (ifPwd != null)
        {
            string pwd = ifPwd.text;
            if (VerificationUserInfo(pwd))
            {
                pwds = pwd;
                return true;
            }
        }
        return false;
    }

    /// <summary>
    /// 验证用户信息
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    private bool VerificationUserInfo(string str)
    {
        if (!IsEmpty(str))
        {
            if (str.Length < 19)
            {
                if ((str.IndexOf(":") == -1) && (str.IndexOf(",") == -1))
                {
                    return true;
                }
            }
        }
        return false;
    }

    /// <summary>
    /// 正则表达式匹配IPv4
    /// </summary>
    /// <param name="str">IPV4地址字符串</param>
    /// <returns>正确与否</returns>
    private bool VerificationIP(string str)
    {
        if (!IsEmpty(str))
        {
            string pattern = @"(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)";
            if (Regex.Match(str, pattern).ToString() == str)
            {
                return true;
            }
            else
            {
                return VerificationDomain(str);
            }
        }
        return false;
    }
    /// <summary>
    /// 正则表达式匹配域名
    /// DNS规定，域名中的标号都由英文字母和数字组成，每一个标号不超过63个字符，也不区分大小写字母。标号中除连字符（-）外不能使用其他的标点符号。级别最低的域名写在最左边，而级别最高的域名写在最右边。由多个标号组成的完整域名总共不超过255个字符。
    /// </summary>
    /// <param name="str">域名字符串</param>
    /// <returns>正确与否</returns>
    private bool VerificationDomain(string str)
    {
        if (!IsEmpty(str))
        {
            string pattern = @"^(?=^.{3,255}$)[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+$";
            if (Regex.Match(str, pattern).ToString() == str)
            {
                return true;
            }
            else if (str == "localhost")
            {
                return true;
            }
        }
        return false;
    }
    /// <summary>
    /// 验证端口号是否正确
    /// </summary>
    /// <param name="str">端口号字符串</param>
    /// <returns>正确与否</returns>
    private bool VerificationPort(string str)
    {
        if (!IsEmpty(str))
        {
            string pattern = @"^[1-9]$|(^[1-9][0-9]$)|(^[1-9][0-9][0-9]$)|(^[1-9][0-9][0-9][0-9]$)|(^[6-6][0-5][0-5][0-3][0-5]$)|(^[1-5][0-9][0-9][0-9][0-9]$)";
            if ((Regex.Match(str, pattern).ToString() == str))
            {
                return true;
            }
        }
        return false;
    }

    /// <summary>
    /// 检查空字符串
    /// </summary>
    /// <param name="str">要检查的字符串</param>
    /// <returns>空与否</returns>
    private bool IsEmpty(string str)
    {
        if (str != null)
        {
            if (str != "")
            {
                return false;
            }
        }
        return true;
    }

}
